package com.wisdytech.linkdcs.configuration.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.wisdytech.common.bean.StandardConfigResult;
import com.wisdytech.common.enums.NgDataType;
import com.wisdytech.linkdcs.configuration.service.IConfigurationDesignService;

/**
 * 组态设计对外接口
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/equipment/configuration/design")
public class ConfigurationDesignAction {

    @Resource
    private IConfigurationDesignService configurationDesignService;
    
    private Logger logger =LoggerFactory.getLogger(ConfigurationDesignAction.class);
    

    /**
     *
     * @param fileKey 主键
     * @param path 路径
     * @param name 文件名
     * @param type 类型
     * @return StandardConfigResult
     */
    @RequestMapping(value = "/getFileContent/get.mvc")
    public StandardConfigResult getFileContent(String fileKey,String name, String type) {
    	logger.info(fileKey+" "+name +" "+type);
        String content = configurationDesignService.getFileContent(fileKey,name,type);
        return StandardConfigResult.getOkResult(content);
    }

    /**
     * 文件保存
     * @param file 文件对象
     * @param path 文件夹路径
     * @param fileKey 组态记录主键
     * @param name 文件名（包含文件扩展名）
     * @param content 文件内容
     * @param type 保存类型
     * @param equipNo 设备编码json
     * @return
     */
    @RequestMapping(value = "/saveFile/save.mvc")
    public StandardConfigResult saveFile( String path, 
    		String fileKey, String name, String content, String type,String equipNo) {
    	boolean result =
    			configurationDesignService.saveFile(path, fileKey, name, content, type,equipNo);
    	
    	return result?StandardConfigResult.getOkResult(""):
    			StandardConfigResult.getErrorResult(NgDataType.SCADA_ERROR.getCode(), 
    					NgDataType.SCADA_ERROR.getName());
    	
    }
    
    /**
     * 文件上传
     * @param path
     * @return
     */
    @RequestMapping(value="/uploadFile/save.mvc")
    public StandardConfigResult uploadFile(@RequestParam(value = "file", required = false) MultipartFile file,
    		String path) {
        String fileName = file.getOriginalFilename();
        
        try {
        	configurationDesignService.uploadFile(file.getBytes(), path, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return StandardConfigResult.getErrorResult(NgDataType.FILE_ERROR.getCode(),
		            			NgDataType.FILE_ERROR.getName());
        }
    	
    	return StandardConfigResult.getOkResult("");
    }
    
    /**
     * 文件名检测
     * @param path
     * @param name
     * @return
     */
    
    @RequestMapping(value="/checkControlName/get.mvc")
    public StandardConfigResult checkControlName(String path,String name) {
    	
    	boolean result =configurationDesignService.checkControlName(path, name);
    	return result?StandardConfigResult.getErrorResult(NgDataType.SCADA_ERROR.getCode(), 
					NgDataType.SCADA_ERROR.getName()):StandardConfigResult.getOkResult("");
    }
    
    
    
    /**
     * 获取所有设备编号
     * @return
     */
    @RequestMapping(value="/getEquipCodes/get.mvc")
    public StandardConfigResult getEquipCodes() {
    	
    	List<Map<String, Object>>  result =
    			configurationDesignService.getEquipCodes();
    	return StandardConfigResult.getOkResult(result);
    }
    
    /**
     * 获取设备所有参数
     * @param data
     * @return
     */
    @RequestMapping(value="/getEquipParams/get.mvc")
    public StandardConfigResult getEquipParams(String equipNo) {
    	List<Map<String, Object>>  result =
    			configurationDesignService.getEquipParams(equipNo);
    	return StandardConfigResult.getOkResult(result);
    }
    
    /**
     * js文件获取
     * @param path
     * @return
     */
    @RequestMapping(value="/getFiles/get.mvc")
    public StandardConfigResult getFiles(String path,String type) {
    	
    	List<Map<String, Object>>  result =
    			configurationDesignService.getFiles(path,type);
    	return StandardConfigResult.getOkResult(result);
    }
    
    @RequestMapping(value="/generateItems/get.mvc")
    public StandardConfigResult generateItems(String path) {
    	List<Map<String, Object>>  result =
    			configurationDesignService.getItemFils(path);
    	return StandardConfigResult.getOkResult(result);
    }
    
    
    

}
